//
//  AdvertisementCell.swift
//  FeastBooking
//
//  Created by Imon on 2017/9/3.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

class AdvertisementCell: UICollectionViewCell, UIScrollViewDelegate {
    
    private var cnt = 0
    private var timer: Timer?
    
    let imageScrollView: AutoScrollView = {
        let scrollView = AutoScrollView()
        return scrollView
    }()
    
    let pageControl: UIPageControl = {
        let page = UIPageControl()
        return page
    }()
    
    var urls: [String]?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        imageScrollView.delegate = self
        self.contentView.addSubview(imageScrollView)
        self.contentView.insertSubview(pageControl, aboveSubview: imageScrollView)
        layoutSubviews()
        createTimer()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        imageScrollView.frame = self.contentView.bounds
        pageControl.frame = CGRect(x: self.contentView.bounds.width / 2 - 50, y: self.contentView.bounds.height - 35, width: 100, height: 30)
        if let urls = urls {
            imageScrollView.createUI(urls: urls)
            pageControl.numberOfPages = urls.count
            imageScrollView.setContentOffset(CGPoint(x: imageScrollView.bounds.width, y: 0), animated: false)
        }
    }
    
    func createTimer() {
        timer = Timer.scheduledTimer(timeInterval: 3.5, target: self, selector: #selector(timerManager), userInfo: nil, repeats: true)
    }
    
    func removeTimer() {
        timer?.invalidate()
    }
    
    func timerManager() {
        //自动无限循环的轮播
        let width = imageScrollView.bounds.width
        if pageControl.currentPage == pageControl.numberOfPages - 1 {
            pageControl.currentPage = 0 //到了最后一页，重新置为第一页
        } else {
            pageControl.currentPage += 1 //不是最后一页则往下翻一页
        }
        imageScrollView.setContentOffset(CGPoint(x: imageScrollView.contentOffset.x + width, y: 0), animated: true)
        if pageControl.currentPage == 0 {
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                self.imageScrollView.setContentOffset(CGPoint(x: width, y: 0), animated: false)
            }
        }
    }
    
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let width = scrollView.bounds.width
        let height = scrollView.bounds.height
        if scrollView.contentOffset.x == 0 {
            scrollView.scrollRectToVisible(CGRect(x: scrollView.contentSize.width - 2 * width, y: 0, width: width, height: height), animated: false)
        } else if scrollView.contentOffset.x == scrollView.contentSize.width - width {
            scrollView.scrollRectToVisible(CGRect(x: width, y: 0, width: width, height: height), animated: false)
        }
        
        pageControl.currentPage = Int(scrollView.contentOffset.x / width) - 1
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        removeTimer()
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        createTimer()
    }
    
    
}

